from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.triggers.cron import CronTrigger
from win10toast import ToastNotifier
import time
import logging
sched = BlockingScheduler()
def setup_logging():
    logging.basicConfig(filename='error.log', level=logging.ERROR,
                      format='%(asctime)s:%(levelname)s:%(message)s')


def pc_toast(header='点饭提醒', text='忘记点饭，可是要饿肚子的哦~'):
    toaster = ToastNotifier()
    time.sleep(0.05)  # 适当休息一下，避免出现提示秒弹的情况
    toaster.show_toast(title=f"{header}", msg=f"{text}", duration=5, threaded=True, icon_path='res/maple.ico')
    while toaster.notification_active():  # toaster.notification_active()判断是否有活动通知显示
        time.sleep(0.005)


def addjob(arr):
    # 点饭提醒
    sched.add_job(pc_toast, CronTrigger(hour='14', minute='30'))
    # 作业提醒
    sched.add_job(pc_toast, CronTrigger(day_of_week='6', hour='20', minute='30'), args=['作业提醒', '今天是内训作业最后一天咯'])
    # 下班提醒
    sched.add_job(pc_toast, CronTrigger(hour='20', minute='50'), args=['下班提醒', '即将下班，提前整理工作内容，按时下班哦！'])
    weektime = "以下时间段已加入开课提醒,"
    for i in arr:
        weektime += f"【周{int(i) + 1}】"
        # 开课提醒
        sched.add_job(pc_toast, CronTrigger(day_of_week=i, hour='14', minute='00'), args=['开课提醒', '别忘记发开课提醒和群公告'])
        # 课后提醒
        sched.add_job(pc_toast, CronTrigger(day_of_week=i, hour='20', minute='30'), args=['课后提醒', '别忘记发课后群公告'])
    pc_toast('行课提醒', weektime)
    # scheduler_thread = threading.Thread(target=start_scheduler)
    # scheduler_thread.start()
    # print("已添加的任务:")
    # for job in sched.get_jobs():
    #     print(job)

    #sched.start()

# def start_scheduler():
#     sched.start()